TERMUX_PKG_HOMEPAGE=https://mega.io/
TERMUX_PKG_DESCRIPTION="Provides non UI access to MEGA services"
TERMUX_PKG_LICENSE="BSD 2-Clause"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION="2.3.0"
TERMUX_PKG_REVISION=1
TERMUX_PKG_SRCURL=git+https://github.com/meganz/MEGAcmd
TERMUX_PKG_SHA256=7570093a085379840a903ce56b3ea201be5b460ea3191bb8a8f3a903e2685c14
TERMUX_PKG_GIT_BRANCH="${TERMUX_PKG_VERSION}_Win"
TERMUX_PKG_AUTO_UPDATE=true
TERMUX_PKG_UPDATE_VERSION_REGEXP="\d+\.\d+\.\d+"
# dbus is required for $PREFIX/var/lib/dbus/machine-id
TERMUX_PKG_DEPENDS="c-ares, cryptopp, dbus, ffmpeg, freeimage, libandroid-glob, libc++, libcurl, libicu, libsodium, libsqlite, libuv, mediainfo, openssl, pcre, readline, zlib"
TERMUX_PKG_EXTRA_CONFIGURE_ARGS="
-DUSE_FFMPEG=ON
-DUSE_PCRE=ON
-DUSE_LIBUV=ON
-DUSE_READLINE=ON
-DUSE_PDFIUM=OFF
-DFULL_REQS=OFF
-DVCPKG_ROOT=FALSE
-DENABLE_MEGACMD_TESTS=OFF
-DWITH_FUSE=OFF
"

termux_step_post_get_source() {
	local s=$(find . -type f ! -path '*/.git/*' -print0 | xargs -0 sha256sum | LC_ALL=C sort | sha256sum | cut -d" " -f1)
	if [[ "${s}" != "${TERMUX_PKG_SHA256}" ]]; then
		termux_error_exit "
		Checksum mismatch for source files!
		Expected = ${TERMUX_PKG_SHA256}
		Actual   = ${s}
		"
	fi

	# We use `config.h` generated by configure script instead of this one.
	local f="sdk/include/mega/config-android.h"
	if [ -f "${f}" ]; then
		rm -f "${f}"
		echo '#error DO NOT INCLUDE ME!' > "${f}"
	else
		termux_error_exit "Source file '${f}' not found."
	fi
}

termux_step_pre_configure() {
	export OBJCXX="$CXX"

	LDFLAGS+=" -landroid-glob -lpcrecpp -lz"
	LDFLAGS+=" $($CC -print-libgcc-file-name)"

	# Fix build against FFmpeg 6.0:
	CPPFLAGS+=" -DCODEC_CAP_TRUNCATED=0"

	# treat Android as Linux like sdl2 package
	rm -rf "$TERMUX_PKG_SRCDIR/sdk/examples"
	find "$TERMUX_PKG_SRCDIR" -type f | \
		xargs -n 1 sed -i \
		-e 's/\([^A-Za-z0-9_]__ANDROID\)\(__[^A-Za-z0-9_]\)/\1_NO_TERMUX\2/g' \
		-e 's/\([^A-Za-z0-9_]__ANDROID\)__$/\1_NO_TERMUX__/g'
}
